<template>
  <div>
    <h2>注册</h2>
    <table class="table table-bordered">
      <tr>
        <td>账号</td>
        <td>
          <input type="text" v-model="sdate.U_Name" />
        </td>
      </tr>
      <tr>
        <td>密码</td>
        <td>
          <input type="password" v-model="sdate.U_Pwd" />
        </td>
      </tr>
      <tr>
        <td></td>
        <td><input type="button" value="注册" @click="add" /></td>
      </tr>
    </table>
  </div>
</template>

<script setup lang="ts">
import { reactive, toRefs } from 'vue';
import axios from 'axios';
import { useRouter, useRoute } from 'vue-router';
let router = useRouter();

let data = reactive({
  sdate: {
    U_Name: '',
    U_Pwd: '',
  },
});

let { sdate } = toRefs(data);

const add = () => {
  if (sdate.value.U_Name == '') {
    alert('用户名不能为空');
    return;
  }
  axios({
    url: 'http://localhost:51249/api/User/Add',
    method: 'post',
    data: sdate.value,
  }).then((res) => {
    console.log(res);
    if (res.data.data == -1) {
      alert('用户名存在');
      return;
    } else {
      alert('注册成功');
      router.push('/login');
    }
  });
};
</script>

<style scoped></style>
